<?php

if (!defined('IN_IA')) {
	exit('Access Denied');
}

class Set_EweiShopV2Page extends PluginWebPage
{
	public function main()
	{
		global $_W;
		global $_GPC;

		$item = pdo_fetch("SELECT * FROM " . tablename('ewei_shop_identity_setting') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
		$up_buy_condition = unserialize($item['up_buy_condition']);

		$identity = pdo_fetchall("SELECT id,levelnum,name FROM " . tablename('ewei_shop_identity') . " WHERE uniacid = :uniacid AND levelnum > 0", array(':uniacid' => $_W['uniacid']));

		if ($_W['ispost']) {
			$id = $_GPC['id'];

			if($_GPC['is_show_mendian_info'] == 1){
				$mendian = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_mendian_set') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
				if($mendian['is_use'] != 1){
					show_json(0, '请先开启门店功能！');
				}
			}

			$data = array(
				'uniacid'			 	=> $_W['uniacid'],
				'is_use_identity' 	 	=> intval($_GPC['is_use_identity']),
				'is_use_member'		 	=> intval($_GPC['is_use_member']),
				'is_use_mendian_member' => intval($_GPC['is_use_mendian_member']),
				'show_type'		  	 	=> intval($_GPC['show_type']),
				'identity_agreement' 	=> trim($_GPC['identity_agreement']),
				'is_show_mendian_info' 	=> intval($_GPC['is_show_mendian_info']),
				'is_use_buy_condition'	=> intval($_GPC['is_use_buy_condition']),
				'up_buy_condition'		=> serialize($_GPC['up_buy_condition']),
				'is_use_order'			=> intval($_GPC['is_use_order']),
				'is_use_self_condition' => intval($_GPC['is_use_self_condition']),
			);
			
			if(empty($item)){

				$data['add_time'] = time();
				pdo_insert('ewei_shop_identity_setting', $data);

				plog('identity.set.add', '添加身份升级的基本设置');

			} else {

				$data['edit_time'] = time();
			
				pdo_update('ewei_shop_identity_setting', $data, array('id' => $item['id']));

				plog('identity.set.edit', '修改身份升级的基本设置');

			}
			
			show_json(1, array('url' => webUrl('identity/set', array('tab' => str_replace('#tab_', '', $_GPC['tab'])))));
		}

		include $this->template();
	}

	public function get_identity(){
		global $_W;
		global $_GPC;
		$levelnum = $_GPC['levelnum'];

		$identity = pdo_fetchall("SELECT id,levelnum,name FROM " . tablename('ewei_shop_identity') . " WHERE uniacid = :uniacid AND levelnum > 0", array(':uniacid' => $_W['uniacid']));

		show_json(1, array('identity'=>$identity));
	}
}

?>
